#include "CAppStateGameOver.h"
#include "CAppStateGame.h"
#include "CAppStateManager.h"
#include "CScore.h"

#include <iostream>
#include <cmath>

CAppStateGameOver CAppStateGameOver::Instance;

CAppStateGameOver::CAppStateGameOver() {
    SurfBlack = NULL;
    Surf_Bubble = NULL;
    Surf_ButtonBG = NULL;
}

void CAppStateGameOver::OnActivate() {
    // Load Simple Logo
    SurfBlack = CSurface::OnLoad("content/image/Black.jpg");
    SDL_SetAlpha(SurfBlack, SDL_SRCALPHA, 20);
    BlackIntens = 0;

    Surf_Bubble = CSurface::OnLoad("content/image/cube.jpg");
    Surf_ButtonBG = CSurface::OnLoad("content/image/game_over.jpg");

    Font = CFont::OnLoad("content/fonts/visitor2.ttf", 40);
    TMP_Score = 0;

    DataButton.clear();
    DataButton.push_back(CButton::OnLoad("content/image/Button/Button1.png", 160, 210 + int(DataButton.size() * 55) - 1, 0, "Save Record" , APPSTATE_GETNAME));
    DataButton.push_back(CButton::OnLoad("content/image/Button/Button1.png", 160, 210 + int(DataButton.size() * 55) - 1, 1, "Main Menu", APPSTATE_MENU));

    DataButton[0].Select = true;
    hot = 0;
}

void CAppStateGameOver::OnDeactivate() {
    SDL_FreeSurface(SurfBlack);
    SurfBlack = NULL;
    SDL_FreeSurface(Surf_Bubble);
    Surf_Bubble = NULL;
    SDL_FreeSurface(Surf_ButtonBG);
    Surf_ButtonBG = NULL;
}

void CAppStateGameOver::OnLoop() {
    if (TMP_Score != CField::Field.Score) {
        TMP_Score = int(sqrt(CField::Field.Score) / 2) + TMP_Score;
        if (TMP_Score > CField::Field.Score)
            TMP_Score = CField::Field.Score;
    }
}

void CAppStateGameOver::OnRender(SDL_Surface* Surf_Display) {
    if (BlackIntens < 7) {
        CSurface::OnDraw(Surf_Display, SurfBlack, 0, 0);
        BlackIntens++;
    }

    CSurface::OnDraw(Surf_Display, Surf_ButtonBG, Surf_Display->w / 2 - Surf_ButtonBG->w / 2, 70);
    CFont::OnDraw(Font, 0, TMP_Score, Surf_Display, 180, 145);

    for (int i = 0; i < int(DataButton.size()); i++)
        DataButton[i].OnDraw(Surf_Display, Surf_Bubble, Font);
}

CAppStateGameOver* CAppStateGameOver::GetInstance() {
    return &Instance;
}

void CAppStateGameOver::SelectButton(int dir) {
    if (DataButton[hot].Select) {
        DataButton[hot].Select = false;
        switch (dir) {
            case UP:
                hot -= 1;
            break;

            case DOWN:
                hot += 1;
            break;
        }
        if (hot < 0)
            hot = DataButton.size() - 1;
        else
            hot %= DataButton.size();
        DataButton[hot].Select = true;
    } else {
        DataButton[0].Select = true;
        hot = 0;
    }
}
void CAppStateGameOver::OnMouseMove(int mX, int mY, int relX, int relY, bool Left,bool Right, bool Middle) {
    for (int i = 0; i < int(DataButton.size()); i++)
        DataButton[i].OnLoop(mX, mY);
}

void CAppStateGameOver::OnLButtonDown(int mX, int mY) {
    for (int i = 0; i < int(DataButton.size()); i++)
        DataButton[i].OnClick(mX, mY);
}

void CAppStateGameOver::OnKeyDown(SDLKey sym, SDLMod mod, Uint16 unicode) {
    switch (sym) {
        case SDLK_UP:
            SelectButton(UP);
        break;

        case SDLK_DOWN:
            SelectButton(DOWN);
        break;

        case SDLK_RETURN:
            CAppStateManager::SetActiveAppState(DataButton[hot].GoState);
        break;

        default: break;
    }
}

